home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1999 #1
/
Amiga Plus 1999 #1.iso
/
System-Boost
/
Workbench
/
ToolAlias
/
Source
/
file.c
next >
Wrap
C/C++ Source or Header
|
1997-02-27
|
3KB
|
147 lines
/*
* File I/O for ToolAlias.
* Reads (and writes?) configuration files.
*
* mws, 4 February 1993
*/
/*
* structure of .config file (assg. line 0 is first line):
*
* line 2n: oldtool
* line 2n+1: newtool
*
* with no leading spaces. MUST be valid format!!!
* (Will be machine-generated in some future version).
*/
#include <dos/dos.h>
#include <proto/dos.h>
#include "list.h"
#ifdef TEST
#define Msg(s) printf("error: %s\n", s)
#else
extern void EasyEasyRequest(char *);
#define Msg(s) EasyEasyRequest(s);
#endif
/* write a string to a file (in binary format) */
/* '\n' is appended */
void
FWriteString(BPTR fh, char *buf)
{
FPuts(fh, buf);
FPutC(fh, '\n');
}
/* read a string to a file (in binary format) - returns success */
/* '\n' is stripped and buf null-terminated; assumes dest large enough */
BOOL
FReadString(BPTR fh, char *buf, LONG bufsiz)
{
UWORD len;
if (FGets(fh, buf, bufsiz))
{
len = strlen(buf);
if (buf[len-1] = '\n')
buf[len-1] = '\0'; /* '\n' --> '\0' */
return TRUE;
}
return FALSE;
}
BOOL
get_config(char *name)
{
static BOOL firsttime = TRUE;
char oldname[256], newname[256];
BPTR fh;
TOOL *tool;
BOOL rc = TRUE;
if (fh = Open(name, MODE_OLDFILE))
{
free_list(); /* clear old list */
tool = get_head();
while (FReadString(fh, oldname, 256))
{
if (FReadString(fh, newname, 256))
{
if (!((tool = add_tool(tool)) &&
set_oldname(tool, oldname) &&
set_newname(tool, newname)))
{
if (tool) rem_tool(tool);
Msg("Memory allocation failed");
rc = FALSE;
break;
}
}
else /* invalid file format */
{
Msg("Invalid config file");
rc = FALSE;
break;
}
}
Close(fh);
}
else if (firsttime) /* failed first time - no worries */
firsttime = FALSE;
else /* failed on reload - warn user */
Msg("Couldn't open config file");
if (!rc) free_list();
return rc;
}
/* save current config to named file */
BOOL
write_config(char *name)
{
TOOL *tool;
BPTR fh;
if (fh = Open(name, MODE_NEWFILE))
{
tool = get_head();
while (tool)
{
FWriteString(fh, tool->oldname);
FWriteString(fh, tool->newname);
tool = tool->next;
}
Close(fh);
return TRUE;
}
Msg("Couldn't open config file");
return FALSE;
}
/******************************************************************************/
#ifdef TEST
#include <stdio.h>
void
main(int argc, char **argv)
{
if (get_config("test.config"))
{
TOOL *tool = get_head();
while (tool)
{
printf("%s -> %s\n", tool->oldname, tool->newname);
tool = tool->next;
}
free_list();
}
else Msg("Couldn't get test.config");
} /* main */
#endif TEST